package weaver.jinpin.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.gson.Gson;
import com.kingdee.bos.webapi.entity.OperateParam;
import com.kingdee.bos.webapi.entity.OperatorResult;
import com.kingdee.bos.webapi.entity.RepoResult;
import com.kingdee.bos.webapi.entity.RepoStatus;
import com.kingdee.bos.webapi.sdk.K3CloudApi;

import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * 拷贝 class 到 %weaver%\ecology\classbean\weaver\jinpin\action <br>
 * 拷贝 properties 到 %weaver%\Resin
 */
public class K3CloudUnAudit extends BaseBean implements Action {
	private Log log = LogFactory.getLog(K3CloudUnAudit.class.getName());
	private String kdFormId;
	
	@Override
	@SuppressWarnings("rawtypes")
	public String execute(RequestInfo requestInfo) {
		//
		String result = Action.FAILURE_AND_CONTINUE;
		String message = "";
		// 当前操作类型 submit:提交 / reject:退回 /drawBack:强制收回
		String src = requestInfo.getRequestManager().getSrc();
		if ("submit".equalsIgnoreCase(src)) {
			result = Action.SUCCESS;
			return result;
		}
		// 请求ID
		String requestid = requestInfo.getRequestid();
		// 表单ID
		int formid = requestInfo.getRequestManager().getFormid();
		// 获取单据编号
		String number = "";
		String retTxt = "";
		try {
			try {
				number = K3CloudAudit.getKingdeeBillNo(formid, requestid);
			} catch (Exception ex) {
				message = "获取金蝶撤销错误：" + ex.getMessage();
				requestInfo.getRequestManager().setMessagecontent(message);
				return result;
			}
			if (number == null || number.isEmpty()) {
				result = Action.SUCCESS;
				return result;
			}
			this.kdFormId = K3CloudAudit.getKdFormId(formid);
			if (kdFormId == null || kdFormId.isEmpty()) {
				message = "金蝶对象标识未定义";
				requestInfo.getRequestManager().setMessagecontent(message);
				return result;
			}
			//
			try {
				// 生成接口参数
				OperateParam param = new OperateParam();
				List<String> numbers = new ArrayList<String>();
				numbers.add(number);
				param.setNumbers(numbers);
				// 调用接口
				K3CloudApi client = new K3CloudApi();
				OperatorResult rst = client.unAudit(kdFormId, param);
				// 返回数据转换为字符串，用于日志
				Gson gson = new Gson();
				retTxt = gson.toJson(rst);
				// 返回数据分析，判断是否操作成功
				RepoResult repo = rst.getResult();
				RepoStatus status = repo.getResponseStatus();
				if (status.isIsSuccess()) {
					message = "调用金蝶撤销成功";
					result = Action.SUCCESS;
				} else {
					message = K3CloudAudit.getErrorMessage("撤销", status);
					requestInfo.getRequestManager().setMessagecontent(message);
				}
			} catch (Exception ex) {
				message = "调用金蝶撤销异常：" + ex.getMessage();
				requestInfo.getRequestManager().setMessagecontent(message);
			}
		} finally {
			String logtxt = "K3CloudUnAudit RequestId:%s, BillNo:%s, Result:%s, Message:%s";
			logtxt = String.format(logtxt, requestid, number, retTxt, message);
			log.info(logtxt);
		}
		return result;
	}
}
